<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id')->autoIncrement('100010');
            $table->string('mobile', 15)->unique()->comment('手机号');
            $table->string('password')->default('')->comment('密码');
            $table->string('pay_password')->default('')->comment('支付密码');
            $table->boolean('enable')->default(1)->comment('是否可用 0=冻结 1=可用');
            $table->enum('sex', ['', 'male', 'famale'])->default('')->comment('性别 0=女 1=男');
            $table->string('name', 40)->default('')->comment('姓名');
            $table->string('english_name', 60)->default('')->comment('英文名');
            $table->string('nickname', 40)->default('')->comment('昵称');
            $table->string('tel')->default('')->comment('固话');
            $table->string('email')->default('')->comment('邮箱');
            $table->string('avatar')->default('')->comment('头像');
            $table->ipAddress('reg_ip')->default('')->comment('注册IP');
            $table->string('reg_region', 10)->default('')->comment('注册地区');
            $table->enum('reg_client', ['web', 'mobile', 'ios', 'android'])->default('web')->comment('注册来源');
            $table->timestamp('last_logined_at')->nullable()->comment('最后登录时间');
            $table->ipAddress('last_logined_ip')->default('')->comment('最后登录IP');
            $table->integer('aid')->default(0)->comment('活动id');
            $table->timestamps();

            $table->index(['name'], 'name');
            $table->index(['last_logined_at'], 'lla');
        });

        $table = config('database.connections.mysql.prefix') . 'users';
        DB::statement("ALTER TABLE {$table} ADD roles SET('buyer', 'seller') NOT NULL DEFAULT 'buyer' COMMENT '身份' AFTER pay_password;");
        DB::statement("ALTER TABLE {$table} ADD authentications SET('idcard', 'mobile', 'email', 'guaranty') COMMENT '认证' AFTER avatar;");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}
